<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">hello</div>
</body>
<script>
  let data = {
    msg: 'hello',
    count: 10
  }
  // 模拟Vue实例
  // Object.defineProperty 接收3个参数  (对象、参数、函数)
  // https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
  let vm = {}
  proxyData(data)

  function proxyData(data) {
    Object.keys(data).forEach(key => {
      Object.defineProperty(vm, key, {
        enumerable: true, // 是否可遍历
        configurable: true, // 是否可配置(是否可以删除或者重新定义)
        get() { // 当获取值的时候执行
          console.log('get', data[key])
          return data[key]
        },
        set(newValue) { // 当设置值的时候执行
          console.log('set', newValue)
          if (newValue === data[key]) {
            return
          }
          data[key] = newValue
          document.querySelector('#app').textContent = data[key]
        }
      })
    })

  }


  vm.msg = 'hello Vue'
  console.log(vm.msg)
</script>

</html>